<template>
	<coreshop-page title="公告详情" mode="left">
		<view class="layout-notice-detail">
			<view class="notice-info">
				<view class="title">{{ state.noticeInfo?.title }}</view>
				<view class="time">{{ state.noticeInfo?.createTime }}</view>
				<view class="content">
					<uv-parse :content="state.noticeInfo?.contentBody"></uv-parse>
				</view>
			</view>
		</view>
	</coreshop-page>
</template>
<script setup lang="ts">
	import { reactive } from 'vue';
	import { onLoad, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';
	import { queryNoticeInfo } from '@/core/api';
	import type { Response, NoticeType } from '@/core/models';
	import { handleShowToast, getShareUrl } from '@/core/utils';
	import { ShareClientEnum, ShareModelEnum, ShareEnum } from '@/core/enum';
	import { shareUrl } from '@/core/consts'

	interface QueryParams {
		id : number;
	}
	const state = reactive<{
		noticeInfo : NoticeType;
		shareUrl : string;
	}>({
		noticeInfo: {},
		shareUrl: "",
	})

	onLoad(async (query : QueryParams) => {
		/** 获取文章详情 */
		getNoticeInfo(Number(query.id));
		/** 获取分享url */
		state.shareUrl = await getShareUrl({
			client: ShareClientEnum.wxMiNiProgram,
			url: shareUrl,
			type: ShareModelEnum.url,
			page: ShareEnum.article,
			params: {
				noticeId: String(query.id),
			}
		})
	})

	/** 获取文章详情 */
	const getNoticeInfo = async (id : number) => {
		uni.showLoading({
			title: "加载中..."
		})
		const noticeInfo : Response<NoticeType> = await queryNoticeInfo({ id, });
		uni.hideLoading();
		if (noticeInfo.status) {
			state.noticeInfo = noticeInfo.data;
		} else {
			handleShowToast(noticeInfo.msg)
		}
	}

	/** 分享 */
	onShareAppMessage(() => {
		return {
			title: state.noticeInfo.title,
			path: state.shareUrl
		}
	});
	onShareTimeline(() => {
		return {
			title: state.noticeInfo.title,
			path: state.shareUrl
		}
	});
</script>
<style lang="scss" scoped>
	@import './detail.scss';
</style>